local M = {}

local resolver = require("components.resolver")

local function createObj(event)
	local cfg = event.cfg

	if uiUtils.isInBounds(gPlatform, event.x, event.y) then
		local obj = resolver.resolverCfg(cfg, true)
		gPlatform.gContent:insert(obj)
		local x, y = gPlatform.gContent:contentToLocal(event.x, event.y)
		obj.x, obj.y = x, y

		obj:addEventListener(
				"tap",
				function(event)
					eventDispatcher:dispatchEvent("chooseObj", {target = obj})
					return true
				end
		)
		obj:addEventListener(
				"touch",
				function(event)
					return true
				end
		)

		eventDispatcher:dispatchEvent("platformAddObj", {target = gPlatform.gContent})
		eventDispatcher:dispatchEvent("chooseObj", {target = obj})
	end
end

function M.init()
	eventDispatcher:addEventListener(
			"compRelease",
			createObj,
			function()
				return not coreDisplay.isDisplayObj(gPlatform.gContent)
			end
	)
end

return M